<?php

namespace App\Http\Resources\Activity\B;

use App\Http\Resources\BaseResource;
use App\Models\Activity\ShopOrderActivityModel;
use App\Models\Activity\ShopOrderActivityShopJoinRelModel;
use App\Models\Activity\ShopOrderActivityShopRelModel;
use App\Models\Shop\ShopModel;
use Carbon\Carbon;
use Illuminate\Http\Request;

class ShopOrderActivityDataStatisticsResource extends BaseResource
{
    /**
     * 所有门店数
     *
     * @var int
     */
    public static int $shopTotalCount = 0;

    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            "id"                   => $this->id,
            "type"                 => $this->type,
            "typeText"             => ShopOrderActivityModel::TYPE_TEXT[$this->type] ?? "-",
            "name"                 => $this->name,
            "expireType"           => $this->expireType,
            "expireTypeText"       => ShopOrderActivityModel::EXPIRE_TYPE_TEXT[$this->expireType] ?? "-",
            "startTime"            => $this->startTime > 0 ? Carbon::createFromTimestamp($this->startTime)
                                                                   ->format("Y-m-d H:i:s") : "-",
            "endTime"              => $this->startTime > 0 ? Carbon::createFromTimestamp($this->endTime)
                                                                   ->format("Y-m-d H:i:s") : "-",
            "intro"                => $this->intro,
            "title"                => $this->title,
            "detail"               => $this->detail,
            "shopType"             => $this->shopType,
            "shopTypeText"         => ShopOrderActivityModel::SHOP_TYPE_TEXT[$this->shopType] ?? "-",
            "remark"               => $this->remark,
            "status"               => $this->status,
            "statusText"           => ShopOrderActivityModel::STATUS_TEXT[$this->status] ?? "-",
            "createdAtText"        => $this->createdAt->format("Y-m-d H:i:s"),
            "updatedAtText"        => $this->updatedAt->format("Y-m-d H:i:s"),
            "shopRelCount"         => $this->getShopRelCount(),
            "shopJoinRelCount"     => $this->shopJoinRelCount,
            "shopNotJoinRelCount"  => $this->getShopRelCount() - $this->getShopRealJoinCount(),
            "shopFeedbackRelCount" => $this->shopFeedbackRelCount
        ];
    }

    /**
     * 获取真实已参加数量
     *
     * @return int
     */
    public function getShopRealJoinCount(): int
    {
        if ($this->shopType == ShopOrderActivityModel::SHOP_TYPE_ALL) {
            return $this->shopJoinRelCount;
        }

        return ShopOrderActivityShopJoinRelModel::query()->whereIn(
            "shopId",
            function ($query) {
                $query->select("shopId")->from((new ShopOrderActivityShopRelModel())->getTable())
                      ->where("shopOrderActivityId", $this->id);
            }
        )->where(
            "shopOrderActivityId", $this->id
        )->where(
            "status", ShopOrderActivityShopJoinRelModel::STATUS_APPLY_IN
        )->count();
    }

    /**
     * @return int|mixed
     */
    public function getShopRelCount()
    {
        return $this->shopType == ShopOrderActivityModel::SHOP_TYPE_ALL ? $this->getShopTotalCount() : $this->shopRelCount;
    }

    /**
     * @return int
     */
    public static function getShopTotalCount(): int
    {
        if (!self::$shopTotalCount) {
            self::$shopTotalCount = ShopModel::query()->count();
        }
        return self::$shopTotalCount;
    }
}
